package com.zq.zqplayer.mvp.main.ui

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import com.ziq.base.base.BaseFragment
import com.zq.customviewlib.HorizontalScrollViewTab
import com.zq.zqplayer.R
import com.zq.zqplayer.bean.TopNavigationItemBean
import com.zq.zqplayer.databinding.FragmentHomeBinding
import com.zq.zqplayer.mvvm.recommend.NewRecommendFragment
import com.ziq.base.extension.binding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class HomeFragment : BaseFragment(R.layout.fragment_home) {

    private val binding : FragmentHomeBinding by binding()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.root
        initData(view, savedInstanceState)
    }

    fun initData(view: View, savedInstanceState: Bundle?) {
        var data: ArrayList<TopNavigationItemBean> = arrayListOf()
//        data.add(object : TopNavigationItemBean("mvp") {
//            override fun getFragment(): Fragment {
//                return RecommendFragment()
//            }
//        })
        data.add(object : TopNavigationItemBean("mvvm") {
            override fun getFragment(): Fragment {
                return NewRecommendFragment()
            }
        })
        data.add(TopNavigationItemBean.getEmptyInstance("王者荣耀"))
        data.add(TopNavigationItemBean.getEmptyInstance( "绝地求生"))
        data.add(TopNavigationItemBean.getEmptyInstance( "LOL"))
        data.add(TopNavigationItemBean.getEmptyInstance( "星秀"))
        data.add(TopNavigationItemBean.getEmptyInstance( "吃鸡手游"))
        data.add(TopNavigationItemBean.getEmptyInstance( "吃喝玩乐"))
        data.add(TopNavigationItemBean.getEmptyInstance( "主机"))
        data.add(TopNavigationItemBean.getEmptyInstance( "CF"))
        data.add(TopNavigationItemBean.getEmptyInstance( "颜值"))
        data.add(TopNavigationItemBean.getEmptyInstance( "二次元"))
        data.add(TopNavigationItemBean.getEmptyInstance( "DNF"))
        data.add(TopNavigationItemBean.getEmptyInstance( "暴雪"))
        data.add(TopNavigationItemBean.getEmptyInstance( "我的世界"))

        var adapter: MyViewPagerAdapter = MyViewPagerAdapter(data, childFragmentManager);
        binding.viewPager.adapter = adapter

        binding.HorizontalScrollViewTab.addData(data as List<HorizontalScrollViewTab.ContentItem>?)
        binding.HorizontalScrollViewTab.setOnHorizontalNavigationSelectListener {
            binding.viewPager.setCurrentItem(it, false)
        }
        binding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {

            var isScrolling: Boolean = false
            var tempPosition: Int = 0;
            var currentPostion: Int = 0;
            override fun onPageScrollStateChanged(state: Int) {
                isScrolling = state == ViewPager.SCROLL_STATE_DRAGGING
                Log.d("ziq", String.format("onPageScrollStateChanged %d  ", state) )
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                if(isScrolling){
                    tempPosition = position
                    currentPostion = binding.viewPager.currentItem
                }
                if(tempPosition == position){
                    if(currentPostion == 0){
                        binding.HorizontalScrollViewTab.setPositionChange(currentPostion, 1, positionOffset)
                    }
                    else if(currentPostion > position){
                        //左滑
                        binding.HorizontalScrollViewTab.setPositionChange(currentPostion, currentPostion - 1, 1 - positionOffset)
                    }else{
                        //右滑
                        binding.HorizontalScrollViewTab.setPositionChange(currentPostion, currentPostion + 1, positionOffset)
                    }
                }
            }

            override fun onPageSelected(position: Int) {
                binding.HorizontalScrollViewTab.setPosition(position)
            }
        })

    }

    class MyViewPagerAdapter(var data:ArrayList<TopNavigationItemBean>, var fm: FragmentManager): FragmentStatePagerAdapter(fm){

        override fun getCount(): Int {
            return data.size
        }

        override fun getItem(position: Int): Fragment {
            return data.get(position).getFragment()
        }

    }

}